/*    */ package com.zimbra.cs.service.mail;
/*    */ 
/*    */ import com.zimbra.common.service.ServiceException;
/*    */ import com.zimbra.common.soap.Element;
/*    */ import com.zimbra.common.soap.Element.KeyValuePair;
/*    */ import com.zimbra.common.soap.MailConstants;
/*    */ import com.zimbra.cs.mailbox.MailServiceException;
/*    */ import com.zimbra.cs.mailbox.Mailbox;
/*    */ import com.zimbra.cs.mailbox.Metadata;
/*    */ import com.zimbra.cs.mailbox.OperationContext;
/*    */ import com.zimbra.soap.ZimbraSoapContext;
/*    */ import java.util.List;
/*    */ import java.util.Map;
/*    */ import org.dom4j.QName;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class SetMailboxMetadata
/*    */   extends MailDocumentHandler
/*    */ {
/*    */   private static final int TOTAL_METADATA_LIMIT = 32768;
/*    */   
/*    */   private static enum SectionNames
/*    */   {
/* 38 */     zwc,  bes;
/*    */     
/*    */     private SectionNames() {} }
/*    */   
/* 42 */   public Element handle(Element request, Map<String, Object> context) throws ServiceException { ZimbraSoapContext zsc = getZimbraSoapContext(context);
/* 43 */     Mailbox mbox = getRequestedMailbox(zsc);
/* 44 */     OperationContext octxt = getOperationContext(zsc, context);
/*    */     
/* 46 */     Element meta = request.getElement("meta");
/* 47 */     String section = meta.getAttribute("section");
/* 48 */     section = section.trim();
/* 49 */     if ((section.length() == 0) || (section.length() > 36) || (section.indexOf(':') < 1))
/* 50 */       throw ServiceException.INVALID_REQUEST("invalid mailbox metadata section name", null);
/* 51 */     SectionNames.valueOf(section.substring(0, section.indexOf(':')));
/*    */     
/* 53 */     Metadata metadata = isModify() ? mbox.getConfig(octxt, section) : null;
/* 54 */     List<Element.KeyValuePair> keyvals = meta.listKeyValuePairs();
/* 55 */     if (!keyvals.isEmpty()) {
/* 56 */       metadata = metadata != null ? metadata : new Metadata();
/* 57 */       for (Element.KeyValuePair kvp : keyvals) {
/* 58 */         String key = kvp.getKey();
/* 59 */         if ((key == null) || (key.equals("")))
/* 60 */           throw ServiceException.INVALID_REQUEST("empty key not allowed", null);
/* 61 */         String val = kvp.getValue();
/* 62 */         if ((val == null) || (val.equals(""))) {
/* 63 */           if (isModify()) {
/* 64 */             metadata.remove(key);
/*    */           } else
/* 66 */             throw ServiceException.INVALID_REQUEST("empty value not allowed", null);
/*    */         } else {
/* 68 */           metadata.put(kvp.getKey(), kvp.getValue());
/*    */         }
/*    */       }
/* 71 */       if (metadata.isEmpty()) {
/* 72 */         metadata = null;
/* 73 */       } else if (metadata.toString().length() > 32768)
/* 74 */         throw MailServiceException.TOO_MUCH_METADATA(32768);
/* 75 */     } else if (isModify()) {
/* 76 */       throw ServiceException.INVALID_REQUEST("empty key/value set not allowed", null);
/*    */     }
/* 78 */     mbox.setConfig(octxt, section, metadata);
/*    */     
/* 80 */     Element response = zsc.createElement(getResponseName());
/* 81 */     return response;
/*    */   }
/*    */   
/*    */   boolean isModify() {
/* 85 */     return false;
/*    */   }
/*    */   
/*    */   QName getResponseName() {
/* 89 */     return MailConstants.SET_MAILBOX_METADATA_RESPONSE;
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/SetMailboxMetadata.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */